<template>
  <div class="main">
    <div class="title">
      <a class="active" href="/login">登录</a>
      <span>·</span>
      <a href="/register">注册</a>
    </div>

    <div class="sign-up-container">
      <el-form ref="userForm" :model="user">

        <el-form-item class="input-prepend restyle" prop="email" :rules="[{ required: true, message: '请输入邮箱', trigger: 'blur' },{validator: checkEmail, trigger: 'blur'}]">
          <div >
            <el-input type="text" placeholder="邮箱" v-model="user.email"/>
            <i class="iconfont icon-phone" />
          </div>
        </el-form-item>

        <el-form-item class="input-prepend" prop="password" :rules="[{ required: true, message: '请输入密码', trigger: 'blur' }]">
          <div>
            <el-input type="password" placeholder="密码" v-model="user.password"/>
            <i class="iconfont icon-password"/>
          </div>
        </el-form-item>

        <div class="btn">
          <input type="button" class="sign-in-button" value="登录" @click="submitLogin()">
        </div>
      </el-form>
      <!-- 更多登录方式 -->
      <div class="more-sign">
        <h6>社交帐号登录</h6>
        <ul>
          <li><a id="weixin" class="weixin" href="http://localhost:8150/weChat/code"><i class="iconfont icon-weixin"/></a></li>
          <li><a id="qq" class="qq" href="#"><i class="iconfont icon-qq"/></a></li>
        </ul>
      </div>
    </div>

  </div>
</template>

<script>

  import '~/assets/css/sign.css'
  import '~/assets/css/iconfont.css'

  import cookie from 'js-cookie'

  import loginApi from '@/api/login'

  export default {
    layout: 'sign',

    data () {
      return {
        "user": {
          "email": "",
          "password": ""
        }
      }
    },
    created() {
      // 若存在cookie，则清空cookie
      if(cookie.get("wuhunyu_token")) {
        cookie.set("wuhunyu_token","");
      }
      if(cookie.get("userInfo")) {
        cookie.set("userInfo","");
      }
    },
    methods: {
      // 邮箱校验
      checkEmail(rule, value, callback) {
        if(!/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(this.user.email)) {
          return new callback(new Error("邮箱格式错误"));
        }
        return callback();
      },
      // 登录方法
      submitLogin() {
        // 校验参数
        // email
        if(!/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(this.user.email)) {
          this.$message.error('邮箱格式错误');
          return ;
        }
        // 密码
        if(!this.user.password) {
          this.$message.error('密码不能为空');
          return ;
        }
        // 发送登录请求
        loginApi.login(this.user)
          .then(res => {
            if(res.data.code==200) {
              // 登录成功
              this.$message({
                message: '登录成功',
                type: 'success'
              });
              // 将token存入cookie中
              cookie.set("wuhunyu_token",res.data.data.token,{
                "domain": "localhost",
                "expires": 7
              });
              // 获取用户详情信息
              loginApi.selectMemberInfo()
                .then(res => {
                  if(res.data.code==200) {
                    // 将详情信息存入cookie
                    cookie.set("userInfo",res.data.data.info,{
                      "domain": "localhost",
                      "expires": 7
                    });
                    // 跳转到首页
                    this.$router.push({"path": "/"});
                  }else {
                    this.$message.error(res.data.message);
                  }
                });
            }else {
              this.$message.error(res.data.message);
            }
          });
      }
    }
  }
</script>
<style>
   .el-form-item__error{
    z-index: 9999999;
  }
</style>
